#!/usr/bin/env bash

# Generic script for downloading programs used by the build system
#
#	Copyright (C) 2014, 2015, 2020, 2021 Leah Rowe <info@minifree.org>
#	Copyright (C) 2015 Patrick "P. J." McDermott <pj@pehjota.net>
#	Copyright (C) 2015, 2016 Klemens Nanni <contact@autoboot.org>
#	Copyright (C) 2022, Caleb La Grange <thonkpeasant@protonmail.com>
#
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

./$(dirname $0)/.gitcheck

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e

$(dirname $0)/resources/scripts/misc/versioncheck

# set this when you want to modify each coreboot tree
# for example, you want to test custom patches
# NODELETE= ./download coreboot
deleteblobs="true"
[ "x${NODELETE+set}" = 'xset' ] && deleteblobs="false"

rm -f "build_error"

download="$(dirname $0)/resources/scripts/download"
extra_dir=""

listprograms() {
	for program in "${download}"/*; do
		printf '%s\n' "${program##*/}"
	done
	if [ "${extra_dir}" != "" ] ; then
		for program in ${extra_dir}/resources/scripts/download/*; do
			if [ -f "${download}/${program}" ] ; then
				continue
			fi
			printf '%s\n' "${program##*/}"
		done
	fi

}

help() {
	# As we handle only one --include-extra-dir for now, if
	# another script wrap this script with --include-extra-dir,
	# the wrapper would end up displaying --include-extra-dir in
	# help too. And if the user passes --include-extra-dir to the
	# wrapper script, then this script won't be able to handle two
	# --include-extra-dir and it would not work.
	if [ "${extra_dir}" == "" ] ; then
		echo "USAGE:	./download [--include-extra-dir <PATH>] <PROGRAM> <OPTIONS>"
	else
		echo "USAGE:	./download <PROGRAM> <OPTIONS>"
	fi

	cat <<- EOF
	possible values for 'program':
	$(listprograms)

	Example:	./download flashrom
	Example:	./download coreboot

	Some program options allow for additional parameters:
	Example:	./download coreboot default
	Example:	./download coreboot x60

	Each program download script should work without extra paramaters, but
	they can use them. For instance, './download coreboot' will download all
	coreboot trees by default, but './download coreboot x60' will only download
	the coreboot tree required for the target: x60

	Each program download script should also accept the --help paramater to
	display the usage of the script.

	Refer to the documentation for more information.

	EOF

	if [ "${extra_dir}" == "" ] ; then
		cat <<- EOF
		--include-extra-dir PATH makes download also look for scripts
		in PATH/resources/scripts/download/. If a script of the same
		name exist in both PATH/resources/scripts/download/ and
		resources/scripts/download/, the first one will be preffered.

		Example:	./download --include-extra-dir ../mydir coreboot

		EOF
	fi
}

die() {
	printf 'Error: %s\n' "${@}" 1>&2
	exit 1
}

run_download_program() {
	path="$1"
	shift 1

	if [ "${deleteblobs}" = "false" ]; then
		NODELETE= "${path}" $@
	else
		"${path}" $@
	fi
}

if [ $# -gt 1 ] && [ "$1" = "--include-extra-dir" ] ; then
	if [ $# -lt 2 ]; then
		die "Missing PATH for --include-extra-dir"
	else
		extra_dir="$2"
		shift 2
	fi
fi

if [ $# -lt 1 ]; then
	help
	die "Please specify arguments."
fi

program="${1}"
shift 1
[ "${program}" = help ] && help && exit 0

if [ "${program}" = "all" ]; then
	for downloadProgram in ${download}/*; do
		if [ -f "${downloadProgram}" ]; then
			# If we have two scripts of the same name, the one in
			# --include-extra-dir will take precendece to enable
			# overriding scripts. So we execute later the one in
			# include-extra-dir instead.
			if [ "${extra_dir}" != "" ] && \
				[ -f "${extra_dir}/${downloadProgram}" ] ; then
					continue
			fi

			run_download_program "${downloadProgram}"
		fi
	done
	for downloadProgram in ${extra_dir}/resources/scripts/download/*; do
		if [ -f "${downloadProgram}" ]; then
			run_download_program "${downloadProgram}"
		fi
	done
	exit 0
elif [ ! -f "${download}/${program}" ] && \
	[ ! -f "${extra_dir}//resources/scripts/download/${program}" ]; then
	help
	die "Invalid argument '${program}'. See: './download help'."
fi

if [ "${extra_dir}" != "" ] && \
	[ -f "${extra_dir}//resources/scripts/download/${program}" ] ; then
	run_download_program "${extra_dir}/resources/scripts/download/${program}" $@
else
	run_download_program "${download}/${program}" $@
fi

exit 0

./$(dirname $0)/.gitcheck clean
